package com.example.modulepicselector.view.activity;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import com.example.modulepicselector.IMoveAndSwipeCallback;
import com.example.modulepicselector.ItemTouchHelperCallback;
import com.example.modulepicselector.R;
import com.example.modulepicselector.adapter.MyPagerAdapter;
import com.example.modulepicselector.adapter.PicSelectAdapter;
import com.example.modulepicselector.internal.entity.Item;

import com.example.modulepicselector.listener.OnSelectPicCallback;
import com.example.modulepicselector.view.widget.MyViewPager;
import com.example.modulepicselector.view.widget.ScaleImageView;
import com.example.tanloadpic.TanLoadPic;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class PagerActivity extends AppCompatActivity implements IMoveAndSwipeCallback, OnSelectPicCallback {
    private List<Item> mItemList;
    //    private ImageView mIvPicShow;
    private MyViewPager vpSelector;
    private ArrayList<View> views = new ArrayList<>();
    private float beforeScale = 1.0f;//之前的伸缩值
    private RecyclerView mRvSelectList;
    private MyPagerAdapter mPagerAdapter;
    private PicSelectAdapter mSelectAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Window window = getWindow();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }

        setContentView(R.layout.activity_pager);


        Intent intent = getIntent();
        mItemList = intent.getParcelableArrayListExtra("select_items");

        initView();
        showPic();
    }


    /**
     * 展示图片的方式
     */
    private void showPic() {
        if (!mItemList.isEmpty()) {

            myScale();
            for (int i = 0; i < mItemList.size(); i++) {
                Item item = mItemList.get(i);
                RelativeLayout relativeLayout = new RelativeLayout(this);
                ScaleImageView imageView = new ScaleImageView(this);
                imageView.setViewPager();

                TanLoadPic.get(this).load(item.getUri()).into(imageView);
                relativeLayout.addView(imageView);
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                imageView.setLayoutParams(params);
                views.add(relativeLayout);
            }
            mPagerAdapter = new MyPagerAdapter(views);
            vpSelector.setAdapter(mPagerAdapter);

        }
    }

    private void initView() {
//        mIvPicShow = findViewById(R.id.iv_show_pic);
        vpSelector = findViewById(R.id.view_pager_selector);
        mRvSelectList = findViewById(R.id.rv_pic_select_list);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRvSelectList.setLayoutManager(layoutManager);

        mSelectAdapter = new PicSelectAdapter(mItemList);
        mSelectAdapter.setOnSelectPicCallback(this);
        mRvSelectList.setAdapter(mSelectAdapter);


        ItemTouchHelperCallback itemTouchHelperCallback = new ItemTouchHelperCallback();
        itemTouchHelperCallback.setiMoveAndSwipeCallback(this);


        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemTouchHelperCallback);
        itemTouchHelper.attachToRecyclerView(mRvSelectList);


    }


    private void myScale() {
    }


    @Override
    public void onMove(int prePosition, int postPosition) {
        Collections.swap(mItemList, prePosition, postPosition);
        Collections.swap(views, prePosition, postPosition);

        mPagerAdapter.setList(views);
        mPagerAdapter.notifyDataSetChanged();
        if (mRvSelectList.getAdapter() != null) {
            mSelectAdapter.setList(mItemList);

            mSelectAdapter.notifyItemMoved(prePosition,postPosition);
            mSelectAdapter. notifyItemRangeChanged(Math.min(prePosition, postPosition), Math.max(prePosition ,postPosition));//受影响的itemd都刷新下


//            mRvSelectList.getAdapter().notifyItemMoved(prePosition, postPosition);
        }





    }



    @Override
    public void onSwiped(int position) {

    }

    @Override
    public void changeTab(int position) {
        vpSelector.setCurrentItem(position);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        System.out.println("可以吗，阿明"
        );


        Intent intent = new Intent();
        intent.putExtra("haha",1);
        setResult(RESULT_OK,intent);
        finish();
    }
}